home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / archvrs / mac / uudecdsr.hqx / uuencode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-13  |  3.5 KB  |  181 lines

  1. /* #ifndef lint
  2. static char sccsid[] = "@(#)uuencode.c  5.3-1 (Berkeley) 9/1/87";
  3. #endif */
  4.  
  5. /* Written by Mark Horton */
  6. /* Modified by ajr (Alan J Rosenthatl,flaps@utcsri.UUCP) to use checksums */
  7. /* Modified by fnf (Fred Fish,well!fnf) to use Keith Pyle's suggestion for
  8.    compatibility */
  9. /* Modified by bcn (Bryce Nesbitt,ucbvax!cogsci!bryce) to enable CTRL-C for
  10.    Amiga Lattice C and add a transparant file size trailer for later check. */
  11. /*    Modified by ctj (Chris Jewell, chrisj@cup.portal.com) for LightSpeed C
  12.     on the Mac. */
  13.  
  14. /*
  15.  * uuencode >outfile [infile] name
  16.  *
  17.  * Encode a file so it can be mailed to a remote system.  This version
  18.  * transparantly adds line checksums and a file size for sanity checks.
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. #define LSC
  25.  
  26. #ifdef    AMIGA            /* Amiga Lattice C */
  27. #undef LSC
  28. #define AMIGA_LATTICE
  29. #define MCH_AMIGA
  30. #define MPU68000
  31. #endif
  32.  
  33. #ifdef unix
  34. #undef LSC
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #endif
  38.  
  39. #define SUMSIZE 64  /* 6 bits */
  40. /* ENC is the basic 1 character encode function to make a char printing */
  41. /* Each output character represents 6 bits of input */
  42. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  43. long    totalsize=0;    /* Used to count the file size because ftell() does
  44.                not return sane results for pipes */
  45.  
  46. #ifdef LSC
  47. _main(argc, argv)
  48. #else
  49. main(argc, argv)
  50. #endif
  51. char **argv;
  52. {
  53.     FILE *in;
  54.     int mode;
  55. #ifdef unix
  56.     struct stat sbuf;
  57. #endif
  58. #ifdef AMIGA_LATTICE
  59.     extern int Enable_Abort;    /* Enable CTRL-C for Lattice */
  60.     Enable_Abort=1;
  61. #endif
  62.  
  63.     /* optional 1st argument */
  64.     if (argc > 2) {
  65. #ifdef LSC
  66.         if ((in = fopen(argv[1], "rb")) == NULL) {
  67. #else
  68.         if ((in = fopen(argv[1], "r")) == NULL) {
  69. #endif
  70.             fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
  71.             fprintf(stderr, "USAGE: uuencode >outfile [infile] name\n");
  72.             exit(10);
  73.         }
  74.         argv++; argc--;
  75.     } else
  76.         in = stdin;
  77.  
  78.     if (argc != 2) {
  79.         fprintf(stderr, "USAGE: uuencode >outfile [infile] name\n");
  80.         exit(11);
  81.     }
  82.  
  83. #ifdef unix
  84.     /* figure out the input file mode */
  85.     fstat(fileno(in), &sbuf);
  86.     mode = sbuf.st_mode & 0777;
  87. #else
  88.     mode = 0644;        /* Default permissions */
  89. #endif
  90.  
  91.     printf("\nbegin %o %s\n", mode, argv[1]);
  92.  
  93.     encode(in, stdout);
  94.  
  95.     printf("end\n");
  96.     printf("size %ld\n",totalsize);
  97.     exit(0);
  98. }
  99.  
  100. /*
  101.  * copy from in to out, encoding as you go along.
  102.  */
  103. encode(in, out)
  104. FILE *in;
  105. FILE *out;
  106. {
  107. #ifndef unix
  108. extern errno;
  109. #endif
  110.     char buf[80];
  111.     int i, n, checksum;
  112.  
  113.     for (;;) {
  114.         /* 1 (up to) 45 character line */
  115.         n = fr(in, buf, 45);
  116. #ifdef LSC
  117.         if (errno == -39)    /* eofErr on Macintosh    */
  118.             errno = 0;        /* no error on any system */
  119. #endif
  120.         putc(ENC(n), out);
  121.  
  122.         checksum = 0;
  123.         for (i=0; i<n; i += 3)
  124.             checksum = (checksum+outdec(&buf[i], out)) % SUMSIZE;
  125.  
  126.         putc(ENC(checksum), out);
  127.         putc('\n', out);
  128.  
  129. #ifndef unix
  130.         /* Error checking under UNIX?? You must be kidding! */
  131.         if (errno) {
  132.             fprintf(stderr, "ERROR: error writing to output\n");
  133.             exit(12);
  134.         }
  135. #endif
  136.         if (n <= 0)
  137.             break;
  138.     }
  139. }
  140.  
  141. /*
  142.  * output one group of 3 bytes, pointed at by p, on file f.
  143.  * return the checksum increment.
  144.  */
  145. int outdec(p, f)
  146. char *p;
  147. FILE *f;
  148. {
  149.     int c1, c2, c3, c4;
  150.  
  151.     c1 = *p >> 2;
  152.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  153.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  154.     c4 = p[2] & 077;
  155.     putc(ENC(c1), f);
  156.     putc(ENC(c2), f);
  157.     putc(ENC(c3), f);
  158.     putc(ENC(c4), f);
  159.  
  160.     return((p[0]+p[1]+p[2]) % SUMSIZE);
  161. }
  162.  
  163. /* fr: like read but stdio */
  164. int
  165. fr(fd, buf, cnt)
  166. FILE *fd;
  167. char *buf;
  168. int cnt;
  169. {
  170.     int c, i;
  171.  
  172.     for (i=0; i<cnt; i++) {
  173.         c = getc(fd);
  174.         if (c == EOF)
  175.             return(i);
  176.         totalsize++;
  177.         buf[i] = c;
  178.     }
  179.     return (cnt);
  180. }
  181.